const Router = require("koa-router");
//访问前缀
const router = new Router({ prefix: "/user" });
const { readArgs } = require("../utils/readArgs");

/**
 * @swagger
 * /user/isLogin:
 *   get:
 *     summary: 获取用户登录状态
 *     tags: [用户]
 *     responses:
 *       200:
 *         description: 成功返回用户列表
 */
router.get("/isLogin", async (ctx) => {
  ctx.body = {
    code: 0,
    data: !!ctx.session.isLogin,
    msg: "false 未登录，true 登录",
  };
});

router.post("/login", async (ctx) => {
  let args = readArgs(); //从文件中获取登录密码
  let code = 0;
  let msg = "登录成功";
  let { password } = ctx.request.body;
  if (password === `${args.password}`) {
    if (ctx.session) ctx.session.isLogin = true;
  } else {
    code = -1;
    msg = "密码错误";
  }
  ctx.body = {
    code,
    msg,
  };
});

/**
 * @swagger
 * components:
 *   schemas:
 *     User:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           example: 1
 *         name:
 *           type: string
 *           example: John
 */

module.exports = router;
